package BookStore.view;


import BookStore.util.Myutil;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class searchJFram extends JFrame {
    public
    JPanel searchmain;
    JPanel spanel1;
    JPanel spanel2;
    JPanel spanel3;
    JPanel spanel4;
    JPanel spanel5;
    JPanel spanel6;
    JPanel spanel7;
    JPanel spanel8;
    JButton writer;
    JButton bnum;
    JButton bname;
    JButton press;
    JButton kind;
    JButton price;
    JButton amount;
    JTextField writerfield;
    JTextField bnumfield;
    JTextField bnamefield;
    JTextField pressfield;
    JTextField kindfield;
    JTextField pricefield;
    JTextField amountfield;
    JTextField myarea;

    public searchJFram(){
        LayoutManager layoutManager = new GridLayout(4,1);
        setLayout(layoutManager);
        setTitle("书店销售系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(400, 400, 485, 343);
        setLocationRelativeTo(null);
        searchmain= new JPanel(new GridLayout(4,1));
        bnum =new JButton("按书号查询");
        bnum.setBounds(100, 10, 300, 100);

        bnum.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String a = null;
                    String bid =  bnumfield.getText();
                    PreparedStatement ps = null;
                    String sql="select * from  BOOK where bid =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, bid);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String BID=rs.getString(1);
                        String NAME=rs.getString(2);
                        String AULTHOR=rs.getString(3);
                        String PRICE=rs.getString(4);
                        String KIND=rs.getString(5);
                        String PRESS=rs.getString(6);
                        String SHULIANG =rs.getString(7);

                        a="|书籍编号："+BID+"\t|书名: "+NAME+"\t|作者； "+AULTHOR+"\t|价格: "+PRICE+"\t|种类: "+KIND+"\t|出版商:"+PRESS+"\t|数量: "+SHULIANG;
                        System.out.println(a);

                    }
                    result_interface result_interface = new result_interface(a.toString());

                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);






                } catch (SQLException ex) {
                    ex.printStackTrace();

                }

            }


        });
        writer = new JButton("按作者查询");
        writer.setBounds(100, 20, 300, 100);
        writer.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String a=null;
                    String writer =  writerfield.getText();
                    PreparedStatement ps = null;
                    String sql="select * from  BOOK where writer =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, writer);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String bnum=rs.getString(1);
                        String bname=rs.getString(2);
                        String writer1=rs.getString(3);
                        String press=rs.getString(4);
                        String kind=rs.getString(5);
                        String price=rs.getString(6);
                        String amount =rs.getString(7);

                        a="bnum："+bnum+"banme: "+bname+"writer； "+writer1+"press: "+press+"kind: "+kind+"price:"+price+"amount: "+amount;
                        System.out.println(a);

                    }
                    result_interface result_interface = new result_interface(a.toString());

                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);






                } catch (SQLException ex) {
                    ex.printStackTrace();

                }

            }

        });
        bname =new JButton("按书名查询");
        bname.setBounds(100, 40,300, 100);
        bname.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){
                    String a=null;
                    java.sql.Statement st = conn.createStatement();
                    String bname =  bnamefield.getText();
                    PreparedStatement ps = null;
                    String sql="select * from  BOOK where bname =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, bname);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String bnum=rs.getString(1);
                        String bname1=rs.getString(2);
                        String writer=rs.getString(3);
                        String press=rs.getString(4);
                        String kind=rs.getString(5);
                        String price=rs.getString(6);
                        String amount =rs.getString(7);

                        a="bnum："+bnum+"bname: "+bname1+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price+"amount: "+amount;
                        System.out.println(a);

                    }
                    result_interface result_interface = new result_interface(a.toString());
                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);






                } catch (SQLException ex) {
                    ex.printStackTrace();

                }
            }

        });

        press=new JButton("按出版社查询");
        press.setBounds(100, 50, 300, 100);
        press.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String press =  pressfield.getText();
                    String a=null;
                    PreparedStatement ps = null;
                    String sql="select * from  BOOK where press =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, press);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String bnum=rs.getString(1);
                        String bname=rs.getString(2);
                        String writer=rs.getString(3);
                        String press1=rs.getString(4);
                        String kind=rs.getString(5);
                        String price=rs.getString(6);
                        String amount =rs.getString(7);

                        a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price+"amount: "+amount;
                        System.out.println(a);

                    }
                    result_interface result_interface = new result_interface(a.toString());
                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);






                } catch (SQLException ex) {
                    ex.printStackTrace();

                }
            }

        });
        kind=new JButton("按书类查询");
        kind.setBounds(100, 60, 300, 100);

        kind.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String kind  =   kindfield.getText();
                    PreparedStatement ps = null;
                    String a=null;
                    String sql="select * from  BOOK where kind =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, kind);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String bnum=rs.getString(1);
                        String bname=rs.getString(2);
                        String writer=rs.getString(3);
                        String press=rs.getString(4);
                        String kind1=rs.getString(5);
                        String price=rs.getString(6);
                        String amount =rs.getString(7);

                        a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind1+"price:"+price+"amount: "+amount;
                        System.out.println(a);
                    }
                    result_interface result_interface = new result_interface(a.toString());
                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);

                    // new results_interface("查询结果： ",a.toString());





                } catch (SQLException ex) {
                    ex.printStackTrace();

                }
            }

        });
        price=new JButton("按价格查询");
        price.setBounds(100, 70, 300, 100);
        price.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String price  =   pricefield.getText();
                    PreparedStatement ps = null;
                    String a=null;
                    String sql="select * from  BOOK where price =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, price);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String bnum=rs.getString(1);
                        String bname=rs.getString(2);
                        String writer=rs.getString(3);
                        String press=rs.getString(4);
                        String kind=rs.getString(5);
                        String price1=rs.getString(6);
                        String amount =rs.getString(7);

                        a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price1+"amount: "+amount;
                        System.out.println(a);

                    }
                    result_interface result_interface = new result_interface(a.toString());
                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);






                } catch (SQLException ex) {
                    ex.printStackTrace();

                }
            }

        });
        price=new JButton("按书类查询");
        price.setBounds(100, 80, 300, 100);
        price.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String price  =   pricefield.getText();
                    PreparedStatement ps = null;
                    String a=null;
                    String sql="select * from  BOOK where price =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1, price);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String bnum=rs.getString(1);
                        String bname1=rs.getString(2);
                        String writer=rs.getString(3);
                        String press=rs.getString(4);
                        String kind=rs.getString(5);
                        String price1=rs.getString(6);
                        String amount =rs.getString(7);

                        a="bnum："+bnum+"banme: "+bname1+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price1+"amount: "+amount;
                        System.out.println(a);

                    }
                    result_interface result_interface = new result_interface(a.toString());
                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);






                } catch (SQLException ex) {
                    ex.printStackTrace();

                }
            }

        });
        amount=new JButton("按数量查询");
        amount.setBounds(100, 90, 300, 100);
        amount.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                try (java.sql.Connection conn = Myutil.getConnection()){

                    java.sql.Statement st = conn.createStatement();
                    String amount  =    amountfield.getText();
                    String a=null;
                    PreparedStatement ps = null;
                    String sql="select * from  BOOK where amount =?";
                    ps=conn.prepareStatement(sql);
                    ps.setString(1,  amount);
                    ResultSet rs=ps.executeQuery();
                    while(rs.next()){
                        String bnum=rs.getString(1);
                        String bname=rs.getString(2);
                        String writer=rs.getString(3);
                        String press=rs.getString(4);
                        String kind=rs.getString(5);
                        String price=rs.getString(6);
                        String amount1 =rs.getString(7);

                        a="bnum："+bnum+"banme: "+bname+"writer； "+writer+"press: "+press+"kind: "+kind+"price:"+price+"amount: "+amount1;
                        System.out.println(a);

                    }
                    result_interface result_interface = new result_interface(a.toString());
                    JOptionPane.showMessageDialog(null,"查询成功","成功",
                            JOptionPane.INFORMATION_MESSAGE);






                } catch (SQLException ex) {
                    ex.printStackTrace();

                }
            }

        });
        spanel1=new JPanel();
        spanel2 =new JPanel();
        spanel3=new JPanel();
        spanel4 =new JPanel();
        spanel5=new JPanel();
        spanel6 =new JPanel();
        spanel7=new JPanel();
        bnumfield=new JTextField(8);
        bnamefield=new JTextField(8);
        writerfield=new JTextField(8);
        pricefield=new JTextField(8);
        kindfield=new JTextField(8);
        pressfield=new JTextField(8);
        amountfield =new JTextField(8);
        spanel1.add(bnum);
        spanel1.add(bnumfield);
        spanel2.add(writer);
        spanel2.add(writerfield);
        spanel3.add(bname);
        spanel3.add(bnamefield);
        spanel4.add(press);
        spanel4.add(pressfield);
        spanel5.add(kind);
        spanel5.add(kindfield);
        spanel6.add(price);
        spanel6.add(pricefield);
        spanel7.add(amount);
        spanel7.add(amountfield);
        add(spanel1);
        add(spanel2);
        add(spanel3);
        add(spanel4);
        add(spanel5);
        add(spanel6);
        add(spanel7);

        setVisible(true);
    }









}

